//
//  LGTabBar.m
//  LuxuryGoods
//
//  Created by 刘星辰 on 2018/5/20.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGTabBar.h"

@implementation LGTabBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype) initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.roundButton.frame = frame;
        self.backgroundColor = [UIColor whiteColor];
        [self.roundButton setBackgroundImage:[UIImage imageNamed:@"tab_jisumai" ] forState:UIControlStateNormal];
        WeakObj(self)
        [self.roundButton bk_addEventHandler:^(id sender) {
            StrongObj(self)
            if (self.roundAction) {
                self.roundAction(self.roundButton);
            }
        } forControlEvents:UIControlEventTouchUpInside];
        [self.roundButton sizeToFit];
        [self addSubview:self.roundButton];
        [self.roundButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(-self.roundButton.height / 2.0);
            make.centerX.equalTo(self);
        }];
    }
    return self;
}

//懒加载
- (UIButton *)roundButton
{
    if (!_roundButton) {
        _roundButton = [[UIButton alloc] init];
    }
    return _roundButton;
}


//- (void)layoutSubviews
//{
//    [super layoutSubviews];
//    int centerx = self.bounds.size.width * 0.5;
//    int centery = self.bounds.size.height * 0.5;
//
//    self.roundButton.left = centerx - 30;
//    self.roundButton.top = centery - 50;
//    Class class = NSClassFromString(@"UITabBarButton");
//    
//    UIView *view = self.subviews[2];
//    if ([view isKindOfClass:class])
//    {
//        Class subClass = NSClassFromString(@"UITabBarSwappableImageView");
//        for (UIView *subView in [view subviews])
//        {
//            if ([subView isKindOfClass:subClass])
//            {
//                subView.hidden = YES;
//            }
//        }
//    }
//    
//    for (UIView *view in self.subviews) {
//        //找到UITabBarButton类型子控件
//        if ([view isKindOfClass:class])
//        {
//            NSLog(@"%@",view.subviews);
//        }
//    }
//}

//响应触摸事件，如果触摸位置位于圆形按钮控件上，则由圆形按钮处理触摸消息
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    //判断tabbar是否隐藏
    if (self.hidden == NO) {
        if ([self touchPointInsideCircle:self.roundButton.center radius:30 targetPoint:point]) {
            //如果位于圆形按钮上，则由圆形按钮处理触摸消息
            return self.roundButton;
        }
        else{
            //否则系统默认处理
            return [super hitTest:point withEvent:event];
        }
    }
    return [super hitTest:point withEvent:event];
}

- (BOOL)touchPointInsideCircle:(CGPoint)center radius:(CGFloat)radius targetPoint:(CGPoint)point
{
    CGFloat dist = sqrtf((point.x - center.x) * (point.x - center.x) +
                         (point.y - center.y) * (point.y - center.y));
    return (dist <= radius);
}


@end
